package com.mocoop.ui;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.cordova.CordovaWebView;
import org.apache.cordova.LOG;

import android.app.Fragment;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

import com.mocoop.activity.R;
import com.mocoop.entity.PageInfoEntity;
import com.mocoop.entity.PageInfoEntity.MocoopStyle;

public class MocoopFragment extends Fragment {

	private View mainView;
	private LinearLayout mainLayout, netStatusClues;
	private RelativeLayout webviewLayout, titleBar, leftLayout, rightLayout;
	private ImageView loadView;
	private TextView titleText, titleStauts;
	private ImageButton leftImgBut, rightImgBut;
	private Button leftBut, rightBut;
	private GifView titleLoading;
	private CordovaWebView cordovaWebView;
	private String launchURL;
	private PageInfoEntity pageInfoEntity;
	private View promptView;
	private View.OnClickListener leftLayoutClickListener;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		if (mainView == null) {
			mainView = inflater.inflate(R.layout.activity_mocoop, null);
			initViews();
		} else {
			ViewGroup parent = (ViewGroup) mainView.getParent();
			if (null != parent) {
				parent.removeView(mainView);
			}
		}

		return mainView;
	}

	private void initViews() {

		mainLayout = (LinearLayout) mainView.findViewById(R.id.mainLayout);
		netStatusClues = (LinearLayout) mainView.findViewById(R.id.netStatusClues);
		titleBar = (RelativeLayout) mainView.findViewById(R.id.titleBar);
		rightLayout = (RelativeLayout) mainView.findViewById(R.id.rightLayout);
		leftLayout = (RelativeLayout) mainView.findViewById(R.id.leftLayout);
		webviewLayout = (RelativeLayout) mainView.findViewById(R.id.webviewLayout);
		loadView = (ImageView) mainView.findViewById(R.id.loadView);

		titleText = (TextView) mainView.findViewById(R.id.titleText);
		titleStauts = (TextView) mainView.findViewById(R.id.titleStatus);
		leftImgBut = (ImageButton) mainView.findViewById(R.id.leftImgBut);
		rightImgBut = (ImageButton) mainView.findViewById(R.id.rightImgBut);
		leftBut = (Button) mainView.findViewById(R.id.leftBut);
		rightBut = (Button) mainView.findViewById(R.id.rightBut);

		titleLoading = (GifView) mainView.findViewById(R.id.titleLoading);

		try {
			webviewLayout.addView(cordovaWebView.getView());
			titleText.setText(pageInfoEntity.getTitle());
			setPageStyle(pageInfoEntity.getMocoopStyle());
		} catch (NullPointerException e) {
			e.printStackTrace();
		}
		leftLayout.setOnClickListener(leftLayoutClickListener);

	}

	public LinearLayout getMainLayout() {
		return mainLayout;
	}

	public void setMainLayout(LinearLayout mainLayout) {
		this.mainLayout = mainLayout;
	}

	//public LinearLayout getNetStatusClues() {
		//return netStatusClues;
//	}

	public void setNetStatusClues(LinearLayout netStatusClues) {
		this.netStatusClues = netStatusClues;
	}

	public RelativeLayout getWebviewLayout() {
		return webviewLayout;
	}

	public void setWebviewLayout(RelativeLayout webviewLayout) {
		this.webviewLayout = webviewLayout;
	}

	public RelativeLayout getTitleBar() {
		return titleBar;
	}

	public void setTitleBar(RelativeLayout titleBar) {
		this.titleBar = titleBar;
	}

	public RelativeLayout getLeftLayout() {
		return leftLayout;
	}

	public void setLeftLayout(RelativeLayout leftLayout) {
		this.leftLayout = leftLayout;
	}

	public RelativeLayout getRightLayout() {
		return rightLayout;
	}

	public void setRightLayout(RelativeLayout rightLayout) {
		this.rightLayout = rightLayout;
	}

	public ImageView getLoadView() {
		return loadView;
	}

	public void setLoadView(ImageView loadView) {
		this.loadView = loadView;
	}

	public TextView getTitleText() {
		return titleText;
	}

	public void setTitleText(TextView titleText) {
		this.titleText = titleText;
	}

	public TextView getTitleStauts() {
		return titleStauts;
	}

	public void setTitleStauts(TextView titleStauts) {
		this.titleStauts = titleStauts;
	}

	public ImageButton getLeftImgBut() {
		return leftImgBut;
	}

	public void setLeftImgBut(ImageButton leftImgBut) {
		this.leftImgBut = leftImgBut;
	}

	public ImageButton getRightImgBut() {
		return rightImgBut;
	}

	public void setRightImgBut(ImageButton rightImgBut) {
		this.rightImgBut = rightImgBut;
	}

	public Button getLeftBut() {
		return leftBut;
	}

	public void setLeftBut(Button leftBut) {
		this.leftBut = leftBut;
	}

	public Button getRightBut() {
		return rightBut;
	}

	public void setRightBut(Button rightBut) {
		this.rightBut = rightBut;
	}

	public GifView getTitleLoading() {
		return titleLoading;
	}

	public void setTitleLoading(GifView titleLoading) {
		this.titleLoading = titleLoading;
	}

	public CordovaWebView getCordovaWebView() {
		return cordovaWebView;
	}

	public void setCordovaWebView(CordovaWebView cordovaWebView) {
		this.cordovaWebView = cordovaWebView;
	}

	public String getLaunchURL() {
		return launchURL;
	}

	public void setLaunchURL(String launchURL) {
		this.launchURL = launchURL;
	}

	public void hideNetStatusClues() {
		netStatusClues.setVisibility(View.GONE);
	}

	public void showNetStatusClues() {
		netStatusClues.setVisibility(View.VISIBLE);
	}

	public View getPromptView() {
		return promptView;
	}

	public void setPromptView(View promptView) {
		this.promptView = promptView;

		RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
		layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
		webviewLayout.addView(promptView, layoutParams);
		webviewLayout.bringChildToFront(promptView);
	}

	public synchronized void loadURL(String url) {
		cordovaWebView.loadUrl(url);
		launchURL = url;
	}

	public void hideLoadView(Animation animation) {
		if (animation != null) {
			loadView.startAnimation(animation);
		}
		loadView.setVisibility(View.GONE);
	}

	public void showLoadView(Animation animation) {
		if (loadView != null) {
			if (animation != null) {
				loadView.startAnimation(animation);
			}
			loadView.setVisibility(View.VISIBLE);
		}
	}

	public void hideTitleStatus() {
		titleLoading.setVisibility(View.GONE);
		titleStauts.setVisibility(View.GONE);
	}

	public void showTitleStatus() {
		titleLoading.setVisibility(View.VISIBLE);
		titleStauts.setVisibility(View.VISIBLE);
	}

	public void hidePromptView() {
		if (null != promptView)
			promptView.setVisibility(View.GONE);
	}

	public void setLeftLayoutEvent(View.OnClickListener leftLayoutClickListener) {
		this.leftLayoutClickListener = leftLayoutClickListener;
	}

	public PageInfoEntity getPageInfoEntity() {
		return pageInfoEntity;
	}

	public void setPageInfo(PageInfoEntity pageInfoEntity) {
		this.pageInfoEntity = pageInfoEntity;
		if (titleText != null) {
			titleText.setText(pageInfoEntity.getTitle());
			setPageStyle(pageInfoEntity.getMocoopStyle());
		}
	}

	@Override
	public void onDestroy() {
		Log.d("RZZ", launchURL + "onDestroy");
		super.onDestroy();
		if (cordovaWebView != null) {
			cordovaWebView.handleDestroy();
		}
	}

	@Override
	public void onStop() {
		Log.d("RZZ", launchURL + "onStop");
		super.onStop();
		if (cordovaWebView != null) {
			cordovaWebView.handleStop();
		}
	}

	@Override
	public void onStart() {
		Log.d("RZZ", launchURL + "onStart");
		super.onStart();
		if (cordovaWebView != null) {
			cordovaWebView.handleStart();
		}
	}

	@Override
	public void onPause() {
		super.onPause();
		Log.d("RZZ", launchURL + "onPause");
		if (cordovaWebView != null) {
			cordovaWebView.handlePause(false);
		}
	}

	@Override
	public void onResume() {
		super.onResume();
		Log.d("RZZ", launchURL + "onResume");
		if (cordovaWebView != null) {
			cordovaWebView.handleResume(false);
		}
	}

	private void setPageStyle(final MocoopStyle mocoopStyle) {
		if (mocoopStyle == null) {
			leftLayout.setVisibility(View.GONE);
			rightLayout.setVisibility(View.GONE);
			return;
		}
		if (mocoopStyle.getTopBar() != null) {

			if (mocoopStyle.getTopBar().getLeftImgBut() != null) {
				leftLayout.setVisibility(View.VISIBLE);
				leftImgBut.setVisibility(View.VISIBLE);
			}else{
				leftImgBut.setVisibility(View.GONE);
			}
			
			if (mocoopStyle.getTopBar().getBarTintColor() != null) {
				titleBar.setBackgroundColor(parseColor(mocoopStyle.getTopBar().getBarTintColor()));
			}
			if (mocoopStyle.getTopBar().getTitleColor() != null) {
				titleText.setTextColor(parseColor(mocoopStyle.getTopBar().getTitleColor()));
			}
			if (mocoopStyle.getTopBar().getLeftButtonItem() != null) {
				leftLayout.setVisibility(View.VISIBLE);
				if (mocoopStyle.getTopBar().getLeftButtonItem().getFontSize() != 0) {
					leftBut.setTextSize(mocoopStyle.getTopBar().getLeftButtonItem().getFontSize());
				}
				if (mocoopStyle.getTopBar().getLeftButtonItem().getTintColor() != null) {
					leftBut.setText(mocoopStyle.getTopBar().getLeftButtonItem().getTitle());
				}
				if (mocoopStyle.getTopBar().getLeftButtonItem().getTintColor() != null) {
					leftBut.setTextColor(parseColor(mocoopStyle.getTopBar().getLeftButtonItem().getTintColor()));
				}
			}else{
				leftBut.setVisibility(View.GONE);
			}

			if(mocoopStyle.getTopBar().getLeftButtonItem() == null && mocoopStyle.getTopBar().getLeftImgBut() == null){
				leftLayout.setVisibility(View.GONE);
			}
			
			
			if (mocoopStyle.getTopBar().getRightImgBut() != null) {
				rightLayout.setVisibility(View.VISIBLE);

				InputStream imgIs = null;
				if (mocoopStyle.getTopBar().getRightImgBut().startsWith("html")) {
					try {
						imgIs = getActivity().getAssets().open(mocoopStyle.getTopBar().getRightImgBut());
						rightImgBut.setBackgroundDrawable(new BitmapDrawable(BitmapFactory.decodeStream(imgIs)));
						imgIs.close();
						rightImgBut.setVisibility(View.VISIBLE);
					} catch (IOException e) {
						e.printStackTrace();
					}
				} else {
					try {
						imgIs = new FileInputStream(mocoopStyle.getTopBar().getRightImgBut());
						rightImgBut.setBackgroundDrawable(new BitmapDrawable(BitmapFactory.decodeStream(imgIs)));
						imgIs.close();
						rightImgBut.setVisibility(View.VISIBLE);
					} catch (IOException e) {
						e.printStackTrace();
					}
				}

				rightLayout.setOnTouchListener(new View.OnTouchListener() {

					@Override
					public boolean onTouch(View v, MotionEvent event) {
						if (event.getAction() == MotionEvent.ACTION_DOWN) {
							cordovaWebView.loadUrl("javascript:cordova.fireDocumentEvent(" + mocoopStyle.getTopBar().getRightEvent() + ");");
						}
						return false;
					}
				});
			} else {
				rightImgBut.setVisibility(View.GONE);
			}

			if (mocoopStyle.getTopBar().getRightButtonItem() != null) {

				rightLayout.setVisibility(View.VISIBLE);
				if (mocoopStyle.getTopBar().getRightButtonItem().getTitle() != null) {
					rightBut.setText(mocoopStyle.getTopBar().getRightButtonItem().getTitle());
					rightBut.setVisibility(View.VISIBLE);
					rightLayout.setOnTouchListener(new View.OnTouchListener() {

						@Override
						public boolean onTouch(View v, MotionEvent event) {
							if (event.getAction() == MotionEvent.ACTION_DOWN) {
								cordovaWebView.loadUrl("javascript:cordova.fireDocumentEvent(" + mocoopStyle.getTopBar().getRightButtonItem().getEventNameForTriggerOnClick()
										+ ");");
							}
							return false;
						}
					});
				} else {
					rightBut.setVisibility(View.GONE);
				}

				if (mocoopStyle.getTopBar().getRightButtonItem().getFontSize() != 0) {
					rightBut.setTextSize(mocoopStyle.getTopBar().getRightButtonItem().getFontSize());
				}

				if (mocoopStyle.getTopBar().getRightButtonItem().getTintColor() != null) {
					rightBut.setTextColor(parseColor(mocoopStyle.getTopBar().getRightButtonItem().getTintColor()));
				}

			} else {
				rightBut.setVisibility(View.GONE);
			}

			if (mocoopStyle.getContentView() != null) {
				if (mocoopStyle.getContentView().getBackGroundColor() != null) {
					loadView.setBackgroundColor(parseColor(mocoopStyle.getContentView().getBackGroundColor()));
				}
			}
		}

	}

	private static int parseColor(String color) {

		if (!color.startsWith("#")) {
			color = "#" + color;
		}

		return Color.parseColor(color);
	}

	public interface MyAnimationCallBack {
		public void onEnd();
	}

	public class MyAnimationLisener implements Animation.AnimationListener {

		private MyAnimationCallBack animationCallBack;

		public MyAnimationLisener(MyAnimationCallBack animationCallBack) {
			super();
			this.animationCallBack = animationCallBack;
		}

		@Override
		public void onAnimationEnd(Animation arg0) {
			animationCallBack.onEnd();
		}

		@Override
		public void onAnimationRepeat(Animation arg0) {

		}

		@Override
		public void onAnimationStart(Animation arg0) {

		}

	}

}